package com.bnq.logc.task

import com.bnq.logc.LogcManager
import com.bnq.logc.data.UploadResult
import com.bnq.logc.upload.UploadFileManager
import java.io.File

class UploadTmpFileTask(private val files:List<File>,private val directUpload:Boolean = false,private val callback:((List<UploadResult>)->Unit)? = null):LogTask() {
    override fun execute() {
        try{
            val existFiles = files.filter { it.exists() }
            if(directUpload){
                uploadFiles(existFiles)
            }else{
                val notRecordFiles = UploadFileManager.filterNotRecordFileInTmp(files)
                uploadFiles(notRecordFiles)
            }
        }catch (e:Exception){
            e.printStackTrace()
            if(LogcManager.getConfig().mIsDebug){
                throw e
            }
        }
    }

    private fun uploadFiles(files: List<File>){
        if(files.isEmpty()) {
            callback?.invoke(emptyList())
            return
        }
        val cloudPathNames = mutableListOf<String>()
        val config = LogcManager.getConfig()
        files.forEach {file->
            cloudPathNames.add("${config.getTmpSimpleCacheLogParentDir()}/${file.name}")
        }
        UploadFileManager.uploadTmpFiles(files,cloudPathNames,callback)
    }
}